<template>
  <div>
    <el-form :model="condition" size="small" :inline="true">
      <el-form-item label="巡检回执编号">
        <el-input v-model="condition.id" placeholder="请输入巡检回执编号" clearable/>
      </el-form-item>
      <el-form-item label="巡检任务编号">
        <el-input v-model="condition.inspectionId" placeholder="请输入巡检任务编号" clearable/>
      </el-form-item>
      <el-form-item label="巡检员">
        <el-input v-model="condition.operationUser" placeholder="请输入巡检员" clearable/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="searchClick">查询</el-button>
        <el-button icon="el-icon-refresh-left" @click="reset()">重置</el-button>
        <el-button icon="el-icon-circle-plus-outline" @click="showInspectionReceptionDialog()">新增</el-button>
      </el-form-item>
    </el-form>
    <div style="height: 34px">
      <el-pagination background align="right" v-show="total > 0"
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="currentPage"
                     :page-sizes="pageSizes"
                     :page-size="pageSize"
                     layout="total, sizes, prev, pager, next"
                     :total="total">
      </el-pagination>
    </div>
    <!-- el-table的data属性就是用来绑定这个表格显示的数据 -->
    <el-table :data="inspectionReceptions" height="250" style="width: 100%">
      <!-- el-table-column表示表格的每一列信息 label属性表示表头的名称
         prop表示绑定的字段名 width属性表示这一列显示的宽度-->
      <el-table-column prop="id" label="巡检回执编号" width="200px"/>
      <el-table-column prop="inspectionId" label="巡检任务编号" width="200px"/>
      <el-table-column prop="defectTypeId" label="缺陷类型编号">
        <template slot-scope="scope">
          <span v-if="scope.row.defectTypeId === 1">叉梁断裂</span> <span
          v-else-if="scope.row.defectTypeId === 2">拦河线断裂</span>
          <span v-else-if="scope.row.defectTypeId === 3">绝缘子爆破</span>
          <span v-else-if="scope.row.defectTypeId === 4">杆塔倾斜</span>
          <span v-else-if="scope.row.defectTypeId === 5">横杠弯曲下垂</span>
          <span v-else-if="scope.row.defectTypeId === 6">瓷瓶破裂</span>
          <span v-else-if="scope.row.defectTypeId === 7">吊杆变形</span>
          <span v-else-if="scope.row.defectTypeId === 8">其他</span>
          <span v-else></span>
        </template>
      </el-table-column>
      <el-table-column prop="defectLevelId" label="缺陷等级编号">
        <template slot-scope="scope">
          <span style="color: #a39d9d" v-if="scope.row.defectLevelId === 1">一般</span>
          <span style="color: orange" v-else-if="scope.row.defectLevelId === 2">严重</span>
          <span style="color: red" v-else-if="scope.row.defectLevelId === 3">紧急</span>
          <span v-else></span>
        </template>
      </el-table-column>
      <el-table-column prop="rate" label="设备的完好率">
        <template slot-scope="scope">
          <span style="color: red" v-if="parseFloat(scope.row.rate.replace('%', '')) < 33">{{ scope.row.rate }}</span>
          <span style="color: orange"
                v-else-if="parseFloat(scope.row.rate.replace('%', '')) < 67">{{ scope.row.rate }}</span>
          <span v-else style="color: green">{{ scope.row.rate }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="state" label="巡检回执状态">
        <template slot-scope="scope">
          <span v-if="scope.row.state === 0">未提交</span>
          <span style="color: orange" v-else-if="scope.row.state === 1">待审核</span>
          <span style="color: mediumspringgreen" v-else>审核通过</span>
        </template>
      </el-table-column>
      <el-table-column prop="operationUser" label="任务巡检员"/>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="showInspectionReceptionDialog(scope.row)">编辑</el-button>
          <!--          <el-button v-if="scope.row.state === 1" type="text" @click="changeUseState(scope.row.id, 1, 0)">停用-->
          <!--          </el-button>-->
          <!--          <el-button v-else type="text" @click="changeUseState(scope.row.id,0, 1)">启用</el-button>-->
          <el-button type="text" @click="deleteInspectionReception(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="`${inspectionReceptionDialog.isUpdate ? '编辑巡检任务回执' : '新增巡检任务回执'}`"
               :visible.sync="inspectionReceptionDialog.visible" width="50%">
      <el-form :model="inspectionReceptionDialog.editInspectionReception"
               :rules="inspectionReceptionDialog.inspectionReceptionRules" ref="inspectionReceptionRules"
               :inline="true">
        <!--        <el-form-item label="巡检回执编号">-->
        <!--          <el-input v-model="inspectionReceptionDialog.editInspectionReception.id"-->
        <!--                    :readonly="inspectionReceptionDialog.isUpdate"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="巡检任务编号">-->
        <!--          <el-input v-model="inspectionReceptionDialog.editInspectionReception.inspectionId"-->
        <!--          />-->
        <!--        </el-form-item>-->
        <el-form-item label="缺陷类型编号">
          <el-select v-model="inspectionReceptionDialog.editInspectionReception.defectTypeId"
                     placeholder="请选择缺陷类型编号" clearable :readonly="inspectionReceptionDialog.isUpdate"
                     style="width: 205px">
            <!--            <el-option v-for="(s,index) in sexArr" :key="index" :label="s" :value="index"></el-option>-->
            <!--            <el-option label="叉梁断裂" value="1"></el-option>-->
            <!--            <el-option label="拦河线断裂" value="2"></el-option>-->
            <!--            <el-option label="绝缘子爆破" value="3"></el-option>-->
            <!--            <el-option label="杆塔倾斜" value="4"></el-option>-->
            <!--            <el-option label="横杠弯曲下垂" value="5"></el-option>-->
            <!--            <el-option label="瓷瓶破裂" value="6"></el-option>-->
            <!--            <el-option label="吊杆变形" value="7"></el-option>-->
            <!--            <el-option label="其他" value="8"></el-option>-->
<!--            <el-option v-for="(s,index) in defectTypeIdArr" :key="index"-->
<!--                       :label="index===0 ? defectTypeIdArr[0] : (index===1 ? defectTypeIdArr[1] : index===2 ? defectTypeIdArr[2] : index===3 ? defectTypeIdArr[3] : index===4 ? defectTypeIdArr[4] : index===5 ? defectTypeIdArr[5] : index===6 ? defectTypeIdArr[6] : index===7 ? defectTypeIdArr[7] :  defectTypeIdArr[8])"-->
<!--                       :value="index+1"></el-option>-->
            <el-option v-for="(s,index) in defectTypeIdArr" :key="index"
                       :label="defectTypeIdArr[index]" :value="index+1"></el-option>
          </el-select>
          <!--          <el-input v-model="inspectionReceptionDialog.editInspectionReception.defectTypeId"-->
          <!--                    :readonly="inspectionReceptionDialog.isUpdate"/>-->
        </el-form-item>
        <el-form-item label="缺陷等级编号">
          <el-select v-model="inspectionReceptionDialog.editInspectionReception.defectLevelId"
                     placeholder="请选择缺陷类型编号" clearable :readonly="inspectionReceptionDialog.isUpdate"
                     style="width: 205px">
<!--            <el-option v-for="(s,index) in defectLevelArr" :key="index"-->
<!--                       :label="index===0 ? defectLevelArr[0] : (index===1 ? defectLevelArr[1] : defectLevelArr[2])"-->
<!--                       :value="index+1"></el-option>-->
            <el-option v-for="(s,index) in defectLevelArr" :key="index"
                       :label="defectLevelArr[index]" :value="index+1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备的完好率" v-if="inspectionReceptionDialog.isUpdate">
          <el-input v-model="inspectionReceptionDialog.editInspectionReception.rate"
          />
        </el-form-item>
        <el-form-item label="任务回执状态" v-if="inspectionReceptionDialog.isUpdate">
          <el-select v-model="inspectionReceptionDialog.editInspectionReception.state"
                     placeholder="请选择缺陷类型编号" clearable :readonly="inspectionReceptionDialog.isUpdate"
                     style="width: 205px">
<!--            <el-option v-for="(s,index) in stateArr" :key="index"-->
<!--                       :label="index===0 ? '未提交' : (index===1 ? '待审核' : '审核通过')" :value="index"></el-option>-->
            <el-option v-for="(s,index) in stateArr" :key="index"
                       :label="stateArr[index]" :value="index"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="任务巡检人员">
          <el-select v-model="inspectionReceptionDialog.editInspectionReception.operationUser"
                     placeholder="请选择巡检人员" clearable :readonly="inspectionReceptionDialog.isUpdate"
                     style="width: 205px">
            <el-option label="魏思橙" value="魏思橙"></el-option>
            <el-option label="博尔特" value="博尔特"></el-option>
            <el-option label="欧文" value="欧文"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="save">保 存</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
import isNumber from "vue-router";

export default {
  name: "InspectionReception",
  data() {
    return {
      stateArr: ['未提交', '待审批', '审批通过'],
      defectLevelArr: ['一般', '严重', '紧急'],
      defectTypeIdArr: ['叉梁断裂', '拦河线断裂', '绝缘子爆破', '杆塔倾斜', '横杠弯曲下垂', '瓷瓶破裂', '吊杆变形', '其他'],
      condition: {
        id: '',
        inspectionId: '',
        defectTypeId: '',
        defectLevelId: '',
        rate: '',
        operationUser: '',
        state: ''

      },
      auditStates: ['待分配', '已分配', '执行中', '已完成'],
      inspectionReceptions: [],
      currentPage: 1, //分页的默认页码应该是第一页
      pageSizes: [30, 50, 100, 150, 200, 400], //可选的每页显示条数
      pageSize: 30, //每页显示条数
      total: 0, //查询的总条数，默认值为0，查询之后才会赋值
      inspectionReceptionDialog: {
        visible: false,
        isUpdate: false,
        editInspectionReception: {
          id: '',
          inspectionId: '',
          operationUser: '',
        },
        inspectionReceptionRules: {
          id: [
            {required: true, message: '巡检回执编号不能为空(ETxxx)', trigger: ['blur', 'change']}
          ],
          inspectionId: [
            {required: true, message: '巡检任务编号不能为空(CNxxxx)', trigger: ['blur', 'change']}
          ],
          operationUser: [
            {required: true, message: '巡检员不能为空', trigger: ['blur', 'change']}
          ]
        }
      }
    }
  },
  methods: {
    handleSizeChange(val) {//这个函数是用来监听每页显示条数的变更
      this.currentPage = 1
      this.pageSize = val
      this.search()
    },
    handleCurrentChange(val) {//这个函数是用来监听当前页码变化
      this.currentPage = val
      this.search()
    },
    search() {
      this.doGet('/inspectionReception', {
        id: this.condition.id,
        inspectionId: this.condition.inspectionId,
        operationUser: this.condition.operationUser,
        currentPage: this.currentPage,
        pageSize: this.pageSize
      }, resp => {
        this.inspectionReceptions = resp.records
        this.total = resp.total
      })
    },
    searchClick() {
      this.doGet('/inspectionReception', {
        id: this.condition.id,
        inspectionId: this.condition.inspectionId,
        operationUser: this.condition.operationUser,
        currentPage: 1,
        pageSize: this.pageSize
      }, resp => {
        this.inspectionReceptions = resp.records
        this.total = resp.total
      })
    },
    changeUseState(id, oldState, newState) {
      this.doPut('/inspectionReception/state', {
        id: id,
        state: newState
      }, resp => {
        if (resp) {
          this.$message.success(`${newState === 0 ? '停用' : '启用'}成功`)
          this.search()
        } else {
          this.$message.error(`${newState === 0 ? '停用' : '启用'}失败`)
        }
      })
    },
    showInspectionReceptionDialog(defectTask) {
      this.inspectionReceptionDialog.editInspectionReception = {
        id: '',
        inspectionId: '',
        defectTypeId: '',
        defectLevelId: '',
        rate: '',
        operationUser: '',
        state: ''
      }
      if (defectTask) {
        this.inspectionReceptionDialog.isUpdate = true
        //首先获取user对象的属性集合，然后遍历这个集合，将每一个属性值拷贝到editInspectionReception对象中
        Object.keys(defectTask).forEach(key => this.inspectionReceptionDialog.editInspectionReception[key] = defectTask[key])
      } else {
        this.inspectionReceptionDialog.isUpdate = false
      }
      this.inspectionReceptionDialog.visible = true
    },
    closeDialog() {
      this.inspectionReceptionDialog.visible = false
      this.inspectionReceptionDialog.editInspectionReception = {
        inspectionId: '',
        defectTypeId: '',
        defectLevelId: '',
        rate: '',
        operationUser: '',
        state: ''
      }
      //重置所有的表单验证，这样可以清除提示信息
      this.$refs['inspectionReceptionRules'].resetFields();
    },
    save() {
      this.$refs['inspectionReceptionRules'].validate(valid => {
        if (valid) {
          var id = this.inspectionReceptionDialog.editInspectionReception.id
          var inspectionId = this.inspectionReceptionDialog.editInspectionReception.inspectionId

          // if (!id.startsWith("ET") || !inspectionId.startsWith("CN")) {
          //   return false;
          // }
          //
          if (this.inspectionReceptionDialog.isUpdate) {
            //这里就可以提交信息去后台服务器保存数据
            this.doPut('/inspectionReception', this.inspectionReceptionDialog.editInspectionReception, resp => {
              if (resp) {
                this.$message.success("更新成功")
                this.closeDialog()
                this.search()
              } else {
                this.$message.error("巡检回执编号已存在")
              }
            })
          } else {
            this.doPost('/inspectionReception', this.inspectionReceptionDialog.editInspectionReception, resp => {
              console.log(resp)
              if (resp) {
                this.$message.success("保存成功")
                this.closeDialog()
                this.search()
              } else {
                this.$message.error("巡检任务编号已存在")
              }
            })
          }
        } else {
          return false
        }
      })
    },
    deleteInspectionReception(inspectionReception) {
      this.showConfirm(`确定要删除巡检任务回执${inspectionReception.id}吗?`, () => {
        //点击确定按钮后才会执行删除动作
        this.doDelete(`/inspectionReception/${inspectionReception.id}`, {}, resp => {
          if (resp) {
            this.$message.success("删除成功")
            this.search()
          } else {
            this.$message.error("删除失败")
          }
        })
      })
    },
    reset() {
      this.condition = {
        name: '',
        state: '',
        defectUser: ''
      }
      this.search()
    }
  },
  mounted() { //这个函数是一个钩子函数，有vue自动调用，在页面完成挂载之后就会调用这个函数
    this.search()
  }
}
</script>

<style scoped>

</style>
